import {
  cancelRegister,
  deleteEvent,
  editEvent,
  publishEvent,
  searchEvent,
  searchEventById,
  searchJoinUser,
  searchPublishEventCount,
  updateEventStatus
} from '@/service/main/event/event'
import { defineStore } from 'pinia'
import type { IEvent, IEventState } from './type'
const useEventStore = defineStore('event', {
  state: (): IEventState => ({
    eventList: [],
    eventTotalCount: 0,
    eventMemberList: [],
    event: {} as IEvent,
    eventCount: []
  }),
  actions: {
    // 发布活动
    async publishEventAction(eventInfo: any) {
      await publishEvent(eventInfo)
    },
    // 编辑活动
    async editEventAction(event_id: number, eventInfo: any) {
      await editEvent(event_id, eventInfo)
    },
    // 删除活动
    async deleteEventAction(event_id: number, eventInfo: any) {
      await deleteEvent(event_id)
      await this.getEventAction(eventInfo)
    },
    // 搜索活动
    async getEventAction(queryInfo: any) {
      const res = await searchEvent(queryInfo)
      this.eventList = res.data.list
      this.eventTotalCount = res.data.totalCount
    },
    // 更新活动状态
    async updateEventStatusAction(
      event_id: number,
      status: string,
      eventInfo: any
    ) {
      await updateEventStatus(event_id, status)
      await this.getEventAction(eventInfo)
    },
    // 根据id查询活动
    async searchEventByIdAction(event_id: number) {
      const res = await searchEventById(event_id)
      this.event = res.data[0]
    },
    // 根据id查询参与的人
    async searchJoinUserAction(event_id: number) {
      const res = await searchJoinUser(event_id)
      this.eventMemberList = res.data
    },
    // 取消报名
    async cancelRegisterAction(event_id: number, user_id: number) {
      await cancelRegister(event_id, user_id)
      this.searchJoinUserAction(event_id)
    },
    // 查询每个社团的活动个数
    async searchPublishEventCountAction() {
      const res = await searchPublishEventCount()
      this.eventCount = res.data
    }
  }
})

export default useEventStore
